package com.ssyan.boot.service.impl;

import com.ssyan.boot.entity.User;
import com.ssyan.boot.service.UserService;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ssyan
 * @Description 业务层实现类
 * @create 2021/8/17 16:56
 * @Version 1.0
 */
@Repository
public class UserServiceImpl implements UserService {

    //模拟持久层
    Map<Integer,User> map = new HashMap<>();
    public UserServiceImpl() {
        map.put(1,new User("张三","男",18));
        map.put(2,new User("李四","男",25));
        map.put(3,new User("小花","女",17));
        map.put(4,new User("小芳","女",21));
    }


    @Override
    public Mono<User> getUserById(Integer id) {
        return Mono.justOrEmpty(map.get(id));
    }

    @Override
    public Flux<User> getAllUser() {
        return Flux.fromIterable(map.values());
    }

    @Override
    public Mono<Void> addUser(Mono<User> userMono) {
        return userMono.doOnNext(person -> {
            Integer id = map.size()+1;
            map.put(id,person);
        }).thenEmpty(Mono.empty());
    }
}
